package edu.scau.mis.cloth.service.impl;

import edu.scau.mis.cloth.dao.GsOrderItemDao;
import edu.scau.mis.cloth.entity.GsDiscount;
import edu.scau.mis.cloth.dao.GsDiscountDao;
import edu.scau.mis.cloth.service.GsDiscountService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (GsDiscount)表服务实现类
 *
 * @author makejava
 * @since 2022-04-04 13:30:09
 */
@Service("gsDiscountService")
public class GsDiscountServiceImpl implements GsDiscountService {
    @Resource
    private GsDiscountDao gsDiscountDao;

    @Resource
    private GsOrderItemDao gsOrderItemDao;


    //    判断是否在优惠表中,是的话返回有多少优惠，不是的话返回0
    @Override
    public int queryDiscount(Integer id){
            int a=0;
            try{
                a=gsDiscountDao.queryDiscount(id);
            }
            catch(Exception e){
            }
            return a;
    }
//    根据传入的订单和商品编号对折扣进行更新，首先查看该商品是否在优惠表中，在的加调用insert进行更新。
    public void updateDiscount(String orderId,Integer id){
           int discount=queryDiscount(id);
           if(discount!=0){
               gsOrderItemDao.update(discount,orderId,id);
           }
    }
    public int changeDiscount(Integer clId,Integer discount){
        return gsDiscountDao.changeDiscount(clId,discount);
    }
    public  int deleteDiscount(Integer clId){
        return gsDiscountDao.deleteDiscount(clId);
    }
    public  int insertDiscount(Integer clId,Integer discount){
        return gsDiscountDao.insertDiscount(clId, discount);
    }
    public List<Map<String,Object>> queryAll(){
        return this.gsDiscountDao.queryAll();
    }

//    更新订单项传入优惠参数

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GsDiscount queryById(Integer id) {
        return this.gsDiscountDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param gsDiscount 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<GsDiscount> queryByPage(GsDiscount gsDiscount, PageRequest pageRequest) {
        long total = this.gsDiscountDao.count(gsDiscount);
        return new PageImpl<>(this.gsDiscountDao.queryAllByLimit(gsDiscount, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param gsDiscount 实例对象
     * @return 实例对象
     */
    @Override
    public GsDiscount insert(GsDiscount gsDiscount) {
        this.gsDiscountDao.insert(gsDiscount);
        return gsDiscount;
    }

    /**
     * 修改数据
     *
     * @param gsDiscount 实例对象
     * @return 实例对象
     */
    @Override
    public GsDiscount update(GsDiscount gsDiscount) {
        this.gsDiscountDao.update(gsDiscount);
        return this.queryById(gsDiscount.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.gsDiscountDao.deleteById(id) > 0;
    }
}
